package com.sun.memo.service.impl;

import com.sun.memo.dao.BackLogDao;
import com.sun.memo.dao.UserDao;
import com.sun.memo.entity.User;
import com.sun.memo.service.IBackLogService;
import com.sun.memo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private BackLogDao backLogDao;

    @Autowired
    private IBackLogService backLogService;

    @Override
    public User findByUsername(String name, String password) {
        User user = userDao.findByUsername(name);
        //用户名不存在
        if(user == null) {
            return null;
        }
        //用户名存在，密码不正确
        if(!user.getPassword().equals(password)) {
            return null;
        }
        return user;
    }
    @Override
    public String saveUser(User user) {
        User newUser = userDao.findByUsername(user.getUsername());
        if(newUser != null) {
            return "用户已存在";
        }else{
            assert false;
            userDao.save(newUser);
            return "ok";
        }
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.findById(id).orElse(null);
    }

}
